package com.zem.imos.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zem.imos.Entity.*;
import com.zem.imos.Entity.Params.ParamsAdmin;
import com.zem.imos.Entity.Params.ParamsDoctor;
import com.zem.imos.Entity.Params.ParamsPatient;
import com.zem.imos.Entity.Params.ParamsSchedule;
import com.zem.imos.Mapper.*;
import com.zem.imos.Util.JWT;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class AdminService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    DoctorMapper doctorMapper;
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    PatientMapper patientMapper;
    @Autowired
    LaborScheduleMapper laborScheduleMapper;

    /**
     * 管理员注册
     * @author ZEM
     * @Time 2021/11/25
     * @Description 注册管理员账号
     */
    public RespEntity adminlogon(ParamsAdmin params)
    {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",params.getUsername());
        if(adminMapper.selectOneAdmin(queryWrapper)!= null)
        {
            RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"用户名已存在"));
            return res;
        }
        if (params.getPassword() != null)
            params.setPassword(DigestUtils.md5DigestAsHex(params.getPassword().getBytes()));

        User user = new User(params);
        Admin admin = new Admin(params);
        userMapper.insert(user);
        admin.setUserId(user.getId());
        adminMapper.insert(admin);
        RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"用户新增成功"),admin);
        return res;
    }


    /**
     * 管理员获取医生列表
     * @author ZEM
     * @Time 2021/11/30
     * @Description 管理员获取医生列表
     */
    public RespEntity getworkersbyadmin(HttpServletRequest request, Page page)
    {
        String token = request.getHeader("token");
        Claims claims = JWT.parseJwt(token);
        if(claims != null)
        {
            System.out.println(claims.get("role"));
            System.out.println(claims.get("userId"));

            if(claims.get("role").equals("管理员"))
            {
                QueryWrapper<Doctor> queryWrapper=new QueryWrapper();
                queryWrapper.eq("user.role", 0);
                IPage<List<ParamsDoctor>> all = doctorMapper.selectDoctorList(page,queryWrapper);
                RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"连接成功"),all);
                return res;
            }
            RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"身份验证错误，需要管理员。"));
            return res;

        }
        RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"获取用户信息失败"));
        return res;

    }

    /**
     * 可筛选的管理员获取医生列表
     * @author ZEM
     * @Time 2021/11/30
     * @Description 可筛选的管理员获取医生列表
     */
    public RespEntity getdotor(HttpServletRequest request, Page page, ParamsDoctor paramsDoctor)
    {
        String token = request.getHeader("token");
        Claims claims = JWT.parseJwt(token);
        if(claims != null)
        {

            if(claims.get("role").equals("管理员"))
            {
                QueryWrapper<Doctor> queryWrapper=new QueryWrapper();
                queryWrapper.eq("role", 0);
                queryWrapper.eq("subdepartment.id", paramsDoctor.getSubdepartmentId());
                IPage<List<ParamsDoctor>> all = doctorMapper.selectDoctorList(page,queryWrapper);
                RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"连接成功"),all);
                return res;
            }
            RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"身份验证错误，需要管理员。"));
            return res;

        }
        RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"获取用户信息失败"));
        return res;

    }


    /**
     * 管理员添加医生账号
     * @author ZEM
     * @Time 2021/11/30
     * @Description 管理员添加医生账号
     */
    public RespEntity addworkersbyadmin(HttpServletRequest request, ParamsDoctor paramsDoctor)
    {
        String token = request.getHeader("token");
        Claims claims = JWT.parseJwt(token);
        if(claims != null)
        {
            if(claims.get("role").equals("管理员"))
            {
                QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("username",paramsDoctor.getUsername());
                if(doctorMapper.selectOneDoctor(queryWrapper)!= null)
                {
                    RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"用户名已存在"));
                    return res;
                }
                if (paramsDoctor.getPassword() != null)
                    paramsDoctor.setPassword(DigestUtils.md5DigestAsHex(paramsDoctor.getPassword().getBytes()));

                User user = new User(paramsDoctor);
                Doctor doctor = new Doctor(paramsDoctor);
                userMapper.insert(user);
                doctor.setUserId(user.getId());
                doctorMapper.insert(doctor);
                RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"用户新增成功"),doctor);
                return res;
            }
            RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"身份验证错误，需要管理员。"));
            return res;

        }
        RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"获取用户信息失败"));
        return res;

    }

    /**
     * 管理员删除医生账号
     * @author ZEM
     * @Time 2021/11/30
     * @Description 管理员删除医生账号
     */
    public RespEntity deleteworkersbyadmin(HttpServletRequest request, Long id)
    {
        String token = request.getHeader("token");
        Claims claims = JWT.parseJwt(token);
        if(claims != null)
        {
            if(claims.get("role").equals("管理员"))
            {
                userMapper.deleteById(id);
                RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"用户删除成功"));
                return res;
            }
            RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"身份验证错误，需要管理员。"));
            return res;

        }
        RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"获取用户信息失败"));
        return res;

    }

    /**
     * 管理员更新医生账号
     * @author ZEM
     * @Time 2021/11/30
     * @Description 管理员更新医生账号
     */
    public RespEntity updateworkersbyadmin(HttpServletRequest request, ParamsDoctor paramsDoctor)
    {
        String token = request.getHeader("token");
        Claims claims = JWT.parseJwt(token);
        if(claims != null)
        {
            if(claims.get("role").equals("管理员"))
            {
                QueryWrapper<Doctor> queryWrapper=new QueryWrapper();
                queryWrapper.eq("username", paramsDoctor.getUsername());
                if(doctorMapper.selectOneDoctor(queryWrapper)!= null)
                {
                    if (paramsDoctor.getPassword() != null)
                        paramsDoctor.setPassword(DigestUtils.md5DigestAsHex(paramsDoctor.getPassword().getBytes()));
                    User user = new User(paramsDoctor);
                    Doctor doctor = new Doctor(paramsDoctor);
                    userMapper.updateById(user);
                    doctor.setUserId(user.getId());
                    doctorMapper.updateById(doctor);
                    RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"用户更新成功"));
                    return res;
                }
                else
                {
                    RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"用户已经存在！"));
                    return res;
                }

            }
            RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"身份验证错误，需要管理员。"));
            return res;

        }
        RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"获取用户信息失败"));
        return res;

    }


    /**
     * 管理员删除患者账号
     * @author ZEM
     * @Time 2021/11/30
     * @Description 管理员删除患者账号
     */
    public RespEntity deleteuserbyadmin(HttpServletRequest request,Long id)
    {
        String token = request.getHeader("token");
        Claims claims = JWT.parseJwt(token);
        if(claims != null)
        {
            if(claims.get("role").equals("管理员"))
            {

                if(userMapper.selectById(id) == null)
                {
                    RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"用户不存在"));
                    return res;
                }
                userMapper.deleteById(id);
                RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"用户删除成功"));
                return res;
            }
            RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"身份验证错误，需要管理员。"));
            return res;

        }
        RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"获取用户信息失败"));
        return res;

    }

    /**
     * 管理员批量删除患者账号
     * @author ZEM
     * @Time 2021/11/30
     * @Description 管理员批量删除患者账号
     */
    public RespEntity deleteuserbyadminbybatches(HttpServletRequest request, List<Long> userlist)
    {
        String token = request.getHeader("token");
        Claims claims = JWT.parseJwt(token);
        if(claims != null)
        {
            if(claims.get("role").equals("管理员"))
            {
                userMapper.deleteBatchIds(userlist);
                RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"用户批量删除成功"));
                return res;
            }
            RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"身份验证错误，需要管理员。"));
            return res;

        }
        RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"获取用户信息失败"));
        return res;

    }

    /**
     * 管理员重置用户的密码
     * @author ZEM
     * @Time 2021/11/30
     * @Description 管理员重置用户的密码
     */
    public RespEntity updateuserbyadmin(HttpServletRequest request, Long id)
    {
        String token = request.getHeader("token");
        Claims claims = JWT.parseJwt(token);
        if(claims != null)
        {

            if(claims.get("role").equals("管理员"))
            {
                User user = userMapper.selectById(id);
                if(user!=null)
                {
                    user.setPassword(DigestUtils.md5DigestAsHex(user.getUsername().getBytes()));
                    userMapper.updateById(user);
                    RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"用户更新成功"));
                    return res;
                }
                else
                {
                    RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"用户不存在！"));
                    return res;
                }

            }
            RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"身份验证错误，需要管理员。"));
            return res;

        }
        RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"获取用户信息失败"));
        return res;

    }

    // 查找所有
    public RespEntity getuserbyadmin(HttpServletRequest request, Page page, ParamsPatient paramsPatient)
    {
        String token = request.getHeader("token");
        Claims claims = JWT.parseJwt(token);
        if(claims != null)
        {
            if(claims.get("role").equals("管理员"))
            {
                QueryWrapper<User> queryWrapper=new QueryWrapper();
                queryWrapper.eq("user.role", 1);
                if(paramsPatient.getUsername()!= null)
                    queryWrapper.eq("username", paramsPatient.getUsername());
                if(paramsPatient.getName()!=null)
                    queryWrapper.eq("name", paramsPatient.getName());
                if(paramsPatient.getGender()!=null)
                    queryWrapper.eq("gender", paramsPatient.getGender());

                IPage <List<ParamsPatient>> all = patientMapper.selectPatientList(page,queryWrapper);
                RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"连接成功"),all);
                return res;
            }
            RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"身份验证错误，需要管理员。"));
            return res;

        }
        RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"获取用户信息失败"));
        return res;
    }

    public RespEntity getadmininfo(HttpServletRequest request)
    {
        String token = request.getHeader("token");
        Claims claims = JWT.parseJwt(token);
        if(claims.get("role").equals("管理员"))
        {
            QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username",claims.getId());
            ParamsAdmin paramsAdmin = adminMapper.selectOneAdmin(queryWrapper);
            paramsAdmin.setClassification("admin ");
            RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"连接成功"),paramsAdmin);
            return res;
        }
        RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"身份验证错误，需要管理员。"));
        return res;
    }

    /**
     * 管理员排班
     * @author ZEM
     * @Time 2021/12/02
     * @Description 管理员排班
     */
    public RespEntity addSchedule(HttpServletRequest request, ParamsSchedule paramsSchedule)
    {
        String token = request.getHeader("token");
        Claims claims = JWT.parseJwt(token);
        if(claims != null)
        {
            if(claims.get("role").equals("管理员"))
            {

                LaborSchedule laborSchedule = new LaborSchedule(paramsSchedule);
                QueryWrapper<LaborSchedule> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("doctor_id",laborSchedule.getDoctorId());
                queryWrapper.eq("period",laborSchedule.getPeriod().getValue());
                queryWrapper.eq("date",laborSchedule.getDate());
                if(laborScheduleMapper.selectCount(queryWrapper)==0)
                {
                    laborScheduleMapper.insert(laborSchedule);
                    RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"排班成功"),laborSchedule);
                    return res;
                }
                else {
                    RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"不能重复排班！"));
                    return res;
                }

            }
            RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"身份验证错误，需要管理员。"));
            return res;

        }
        RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"获取用户信息失败"));
        return res;

    }

    /**
     * 管理员获取排班时间
     * @author ZEM
     * @Time 2021/12/02
     * @Description 管理员获取排班时间
     */
    public RespEntity selectSchedule(HttpServletRequest request, Long date, Long subDepartmentId )
    {
        String token = request.getHeader("token");
        Claims claims = JWT.parseJwt(token);
        if(claims != null)
        {
            if(claims.get("role").equals("管理员"))
            {

                QueryWrapper queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("date",date);
                queryWrapper.eq("subdepartment_id",subDepartmentId);
                List<Map> schedule = laborScheduleMapper.selectSchedule(queryWrapper);
                if (schedule.size()!=0)
                {
                    RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"查找排班表成功！"),schedule);
                    return res;
                }
                else
                {
                    RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"为空！"),schedule);
                    return res;
                }


            }
            RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"身份验证错误，需要管理员。"));
            return res;

        }
        RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"获取用户信息失败"));
        return res;

    }

    /**
     * 管理员删除排班信息
     * @author ZEM
     * @Time 2021/12/02
     * @Description 管理员删除排班信息
     */
    public RespEntity deleteSchedule(HttpServletRequest request, Long id )
    {
        String token = request.getHeader("token");
        Claims claims = JWT.parseJwt(token);
        if(claims != null)
        {
            if(claims.get("role").equals("管理员"))
            {
                laborScheduleMapper.deleteById(id);
                RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"删除排班表成功！"));
                return res;
            }
            RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"身份验证错误，需要管理员。"));
            return res;

        }
        RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"获取用户信息失败"));
        return res;

    }





}




